# encoding=utf8
import sys
reload(sys)
sys.setdefaultencoding('utf8')

class FT_079c:
    result=[]
    analysisDecription=['自定义消息体长度：','故障码ECU个数：','故障码采集时间：','故障源与故障列表：']
    erro_list=['偏移时间：','故障码源地址：','故障码属性：','故障码数据项个数：','本地址故障列表:']
    des_len=[2,1,8,0]
    error_list_len=[2,1,1,1,5] #5*n
    error_list_str=""
    def LogicAisle(self, protocolStr, header):
        print(protocolStr)
        count=0
        for i in range(len(self.analysisDecription)):
            s=protocolStr[count:count+self.des_len[i]*2]
            print(s)
            if i==0:
                data_len=str(int(s,16))
                header.append(self.analysisDecription[i]+data_len+"\n")
            elif i==1:
                ecu_total=int(s,16)
                header.append(self.analysisDecription[i] + str(ecu_total) + "\n")
            elif i==2:
                error_get_time=str(int(s,16))
                header.append(self.analysisDecription[i] + error_get_time + "ms\n")
            elif i==3:
                header.append(self.analysisDecription[i]+"\n")
                self.error_list_str=protocolStr[count:]
            count += self.des_len[i]*2
        s=self.error_list_str
        while(len(s)>4):
            count=0
            vary_time=int(s[count:count+self.error_list_len[0]*2],16)*0.01
            count = count+ self.error_list_len[0]*2
            header.append(self.erro_list[0]+str(vary_time)+"s\n")
            ero_s_address=s[count:count+self.error_list_len[1]*2]
            count=count + self.error_list_len[1] * 2
            msg=" 数据来源CAN_ID：0x18FECA"+ero_s_address.upper()
            header.append(self.erro_list[1] + ero_s_address +""+ msg + "\n")
            ero_attrib=s[count:count+self.error_list_len[2]*2]
            count=count + self.error_list_len[2] * 2
            if ero_attrib=="00":
                msg=" 未知"
            elif ero_attrib=="01":
                msg=" 康明斯系统ISG"
            elif ero_attrib=="02":
                msg=" 康明斯系统ISF"
            elif ero_attrib=="03":
                msg=" 潍柴系统"
            header.append(self.erro_list[2] + ero_attrib + msg +"\n")
            ero_number=int(s[count:count+self.error_list_len[3]*2],16)
            count=count + self.error_list_len[3] * 2
            header.append(self.erro_list[3] + str(ero_number) + "\n")
            for j in range(ero_number):
                loc_ero_list=s[count:count+self.error_list_len[4]*2]
                count += self.error_list_len[4]*2
                header.append(self.erro_list[4]+loc_ero_list+"\n")
                self.set_locero(loc_ero_list,header)
            header.append("\n")
            s=s[count:]
        self.result=header

    def set_locero(self, s, header):
        byte_0 = s[0:2]
        byte_1 = s[2:4]
        byte_2 = s[4:6]
        byte_3 = s[6:8]
        byte_4 = s[8:10]
        byte0_bin=self.hex_to_bin(byte_0,8)
        header.append("故障码数变类型：\n")
        if byte0_bin[7]=="1":
            header.append("新增\n")
        if byte0_bin[6]=="1":
            header.append("次数变化：\n")
        if byte0_bin[5]=="1":
            header.append("消失\n")
        header.append("SPN有效位中的最低字节：" + byte_1 + "\n")
        header.append("SPN有效位中的第2个字节：" + byte_2 + "\n")
        byte3_bin = self.hex_to_bin(byte_3, 8)
        fmi_bin = byte3_bin[-5:]
        header.append("FMI:" + fmi_bin + "\n")
        spn_high = byte3_bin[0:3]
        header.append("SPN有效位中的最高3位：" + spn_high + "\n")
        byte4_bin = self.hex_to_bin(byte_4, 8)
        spn_mode = byte4_bin[0]
        header.append("SPN转换模式：" + spn_mode + "\n")
        ero_countnum = int(byte4_bin[1:], 2)
        header.append("当前故障计数：" + str(ero_countnum) + "\n")

    def hex_to_bin(self, s, s_len):
        s = bin(int(s, 16))
        s = s[2:]
        s = s.zfill(s_len)
        return s
        
        
